/**
 *
 */
package com.wedroid.r2d2.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericJdbcSupport;
import com.wedroid.entity.contact.Contacter;

/**
 * @author lute
 */
@Repository("contacterDaoSupport")
public class ContacterDaoSupport {

    @Autowired
    private GenericJdbcSupport genericJdbcSupport;

    /**
     * @param droidId
     * @param contacterName
     * @return
     */
    public Contacter getContacter(Long droidId, String contacterName) {
        String sentence = "SELECT id AS id, nick_name AS 'profile.nick_name', remark_name AS 'profile.remark_name', display_name AS 'profile.display_name', head_img_url AS 'profile.head_img_url', contact_flag AS 'profile.contact_flag', member_count AS 'profile.member_count', alias AS 'profile.alias', sex AS 'profile.sex', province AS 'profile.province', city AS 'profile.city', signature AS 'profile.signature', contacter_type AS contacter_type, last_update_time AS last_update_time, droid_id AS 'droid.id' FROM contacter WHERE droid_id = :droidId AND nick_name = :contacterName";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("droidId", droidId);
        parameters.put("contacterName", contacterName);
        return genericJdbcSupport.queryForObject(sentence, parameters, Contacter.class);
    }

    /**
     * @param contacter
     * @return
     */
    public Long createContacter(Contacter contacter) {
        String sentence = "INSERT INTO contacter (nick_name, remark_name, display_name, head_img_url, contact_flag, member_count, alias, sex, province, city, signature, contacter_type, last_update_time, droid_id) VALUES (:profile.nickName, :profile.remarkName, :profile.displayName, :profile.headImgUrl, :profile.contactFlag, :profile.memberCount, :profile.alias, :profile.sex, :profile.province, :profile.city, :profile.signature, :contacterType, :lastUpdateTime, :droid.id)";
        Long id = (Long) genericJdbcSupport.save(sentence, contacter);
        return id;
    }

    /**
     * @param contacter
     */
    public void updateContacter(Contacter contacter) {
        String sentence = "UPDATE contacter set remark_name = :profile.remarkName, display_name = :profile.displayName, head_img_url = :profile.headImgUrl, contact_flag = :profile.contactFlag, member_count = :profile.memberCount, alias = :profile.alias, sex = :profile.sex, province = :profile.province, city = :profile.city, signature = :profile.signature, contacter_type = :contacterType, last_update_time = :lastUpdateTime WHERE id = :id";
        genericJdbcSupport.executeSQL(sentence, contacter);
    }

}
